package com.peng.wx_official_account.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dtflys.forest.Forest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class WeatherService {

    /**
     * Gets weather.
     * 获得天气信息   目前该接口获得的天气信息是定死的,通过修改请求url中的location参数来改变地区信息（郑州新郑）
     *
     * @return the weather
     */
    public Map<String, String> getWeather(String location) {
        String url = "https://devapi.qweather.com/v7/weather/now?key=464b21b8858a4e66b6a946d0e5363853&location=" + location;

        //获得天气信息
        String response = Forest.get(url).execute(String.class);

        JSONObject parseObj = JSONUtil.parseObj(response);

        //存放的具体信息
        String now = parseObj.getStr("now");

        JSONObject info = JSONUtil.parseObj(now);

        HashMap<String, String> map = new HashMap<>();
        map.put("temperature", info.getStr("temp"));
        map.put("weather", info.getStr("text"));

        return map;
    }
}
